class Roll < ActiveRecord::Base
  belongs_to  :fabric
  belongs_to  :roll_state
  belongs_to  :loom
  belongs_to  :packaging_type
  belongs_to  :doffer, :class_name => "User", :foreign_key => :doffed_by_id
  has_many    :roll_defects, :dependent => :destroy
  has_many    :loom_loads, :dependent => :destroy
  has_many    :barcodes, :as => :resource
  
  validates_presence_of  :roll_state
  

  def before_create
    self.lot_number = Time.now.to_i
    self.roll_state = RollState.find_by_code(0)
  end
    
  def calulate_weights
    result = false
    if !self.packaging_type.nil? && !self.length.nil?
      self.net_weight = self.fabric.ounces_per_yard * self.length
      self.gross_weight = self.net_weight + self.packaging_type.weight
      result = true
    end
    result
  end
  
  # Add state methods
  def is_created
    self.roll_state.code == 0
  end
  
  def is_weaving
    self.roll_state.code == 1
  end
  
  def is_woven
    self.roll_state.code == 2
  end
  
  def is_doffed
    self.roll_state.code == 3
  end
  
  def is_inventory
    self.roll_state.code == 4
  end
  
  def is_packed
    self.roll_state.code == 5
  end
  
  def is_shipped
    self.roll_state.code == 6
  end
    
end
